package com.central.scheduler.service.queue;

import com.central.scheduler.service.queue.entity.TaskExecutionContext;
import com.central.scheduler.common.enums.Priority;

import java.util.Map;
import java.util.Objects;

/**
 * @author Tindy
 * @date 2021/12/20
 * @describe
 *  task priority info
 */
public class TaskPriority implements Comparable<TaskPriority> {

    /**
     * processInstancePriority
     */
    private Priority processInstancePriority;

    /**
     * processInstanceId
     */
    private Long processInstanceId;

    /**
     * taskInstancePriority
     */
    private Priority taskInstancePriority;

    /**
     * taskId
     */
    private Long taskId;

    /**
     * taskExecutionContext
     */
    private TaskExecutionContext taskExecutionContext;

    /**
     * workerGroupId
     */
    private Long workerGroupId;

    /**
     * context
     */
    private Map<String, String> context;

    public TaskPriority(){}

    public TaskPriority(Priority processInstancePriority,
                        Long processInstanceId,
                        Priority taskInstancePriority,
                        Long taskId, Long workerGroupId) {
        this.processInstancePriority = processInstancePriority;
        this.processInstanceId = processInstanceId;
        this.taskInstancePriority = taskInstancePriority;
        this.taskId = taskId;
        this.workerGroupId = workerGroupId;
    }

    public Priority getProcessInstancePriority() {
        return processInstancePriority;
    }

    public void setProcessInstancePriority(Priority processInstancePriority) {
        this.processInstancePriority = processInstancePriority;
    }

    public Long getProcessInstanceId() {
        return processInstanceId;
    }

    public void setProcessInstanceId(Long processInstanceId) {
        this.processInstanceId = processInstanceId;
    }

    public Priority getTaskInstancePriority() {
        return taskInstancePriority;
    }

    public void setTaskInstancePriority(Priority taskInstancePriority) {
        this.taskInstancePriority = taskInstancePriority;
    }

    public Long getTaskId() {
        return taskId;
    }

    public Map<String, String> getContext() {
        return context;
    }

    public void setTaskId(Long taskId) {
        this.taskId = taskId;
    }

    public Long getWorkerGroupId() {
        return workerGroupId;
    }

    public void setWorkerGroupId(Long workerGroupId) {
        this.workerGroupId = workerGroupId;
    }

    public void setContext(Map<String, String> context) {
        this.context = context;
    }

    public TaskExecutionContext getTaskExecutionContext() {
        return taskExecutionContext;
    }

    public void setTaskExecutionContext(TaskExecutionContext taskExecutionContext) {
        this.taskExecutionContext = taskExecutionContext;
    }

    @Override
    public int compareTo(TaskPriority other) {
        if (this.getProcessInstancePriority().getCode() > other.getProcessInstancePriority().getCode()) {
            return 1;
        }
        if (this.getProcessInstancePriority().getCode() < other.getProcessInstancePriority().getCode()) {
            return -1;
        }

        if (this.getProcessInstanceId() > other.getProcessInstanceId()) {
            return 1;
        }
        if (this.getProcessInstanceId() < other.getProcessInstanceId()) {
            return -1;
        }

        if (this.getTaskInstancePriority().getCode() > other.getTaskInstancePriority().getCode()) {
            return 1;
        }
        if (this.getTaskInstancePriority().getCode() < other.getTaskInstancePriority().getCode()) {
            return -1;
        }

        if (this.getTaskId() > other.getTaskId()) {
            return 1;
        }
        if (this.getTaskId() < other.getTaskId()) {
            return -1;
        }

        return this.getWorkerGroupId().compareTo(other.getWorkerGroupId());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        TaskPriority that = (TaskPriority) o;
        return processInstancePriority == that.processInstancePriority
                &&  processInstanceId.equals(that.processInstanceId)
                && taskInstancePriority == that.taskInstancePriority
                && taskId.equals(that.taskId)
                && workerGroupId.equals(that.workerGroupId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(processInstancePriority, processInstanceId, taskInstancePriority, taskId, workerGroupId);
    }
}
